

#include "client.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// gcc test_case.c client.c -o test_case -I./

void test_case_one();

void test_case_two();

void test_case_three();

int main(int argc, char * argv[]) {

    // test_case_one();
    // test_case_two();
    test_case_three();
    return EXIT_SUCCESS;
}

/*测试check_address_and_port是否可靠*/
void test_case_one() {

    const char * ip = "220.181.38.148";
    const uint16_t port = 80;

    if(check_address_and_port(ip, port) == 0) {

        printf("check success\n");
    }
}

void test_case_two() {

    const char * ip = "google.com";
    struct hostent * host = gethostbynameinformation(ip);

    if (host == NULL) {

        return;
    }
    printf("%s\n", host->h_name);
    for(int index = 0; host->h_addr_list[index]; index++) {

        printf("%d\n", index);
        char buffer[32];
        bzero(buffer, 32);
        inet_ntop(host->h_addrtype, host->h_addr_list[index], buffer, 32);
        printf("%s\n", buffer);
    }
}

void test_case_three() {

    const char * ip = "www.baidu.com";
    const uint16_t port = 80;
    socket_t sockfd = 0;
    if ((sockfd = connect_to_server(ip, port)) != -1) {

        printf("success\n");
        char request[1000];
        bzero(request,1000);
        int len = snprintf(request,1000,"GET / HTTP/1.1\r\nUser-Agent: chrome/7.58.0\r\nAccept: */*\r\n\r\n");

        write(sockfd, request, len);
        char response[1500];

        int loopStatus = 1;
        do {

            bzero(response, 1500);
            ssize_t size = recv(sockfd, response, 1500, 0);
            printf("recv size %ld\n", size);
            printf("%s\n", response);

        } while(loopStatus);
        close(sockfd);
    }
}

void test_case_four() {

    const char * ip = "instance-1b80fy1h";
    uint16_t port   = 8080;

    if(connect_to_server(ip, port) != -1) {

        
    }    
}